﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Meraz.Core.Parsers
{
    public class HuffingtonPostParser : RegexBlogParser
    {
        public HuffingtonPostParser() { this.BlogKey = BlogKeyType.HuffingtonPost; }
        internal override List<string> ParseAliases(string source)
        {
            //<meta name="author" content="AP" />
            List<string> aliases = new List<string>();
            string authorPattern = @"<meta name=""author"" content=""(?<AUTHOR>.+)"" />";
            Regex authorRegex = new Regex(authorPattern, RegexOptions.IgnoreCase);
            Match match = authorRegex.Match(source);
            if (match.Success)
            {
                string[] tokens = match.Groups["AUTHOR"].Value.Split(',');
                aliases.AddRange(tokens);
            }
            return aliases;
        }

        internal override DateTime ParseDate(string source)
        {
            //<meta name="publish_date" content="Sun, 01 Aug 2010 19:08:26 EST" /> 
            string dateString = null;
            string dateStringPattern = @"<meta name=""publish_date"" content=""(?<DATE>.+)"" />";
            Regex dateStringRegex = new Regex(dateStringPattern, RegexOptions.IgnoreCase);
            Match match = dateStringRegex.Match(source);
            if (match.Success)
            {
                dateString = match.Groups["DATE"].Value;
            }
            DateTime result;
            if (DateTime.TryParse(dateString, out result))
                return result;

            string datePattern = @"^(?<DATETIME>\w\w\w, \d\d \w\w\w \d\d\d\d \d\d:\d\d:\d\d) (?<ZONE>\w+)$";
            Regex dateRegex = new Regex(datePattern);
            Match dateMatch = dateRegex.Match(dateString);
            if (dateMatch.Success)
            {
                string dt = dateMatch.Groups["DATETIME"].Value;
                string zone = GetTimeZoneOffset(dateMatch.Groups["ZONE"].Value);
                if (DateTime.TryParse(string.Format("{0} {1}", dt, zone), out result))
                    return result;

            }
            return DateTime.MinValue;
        }

        internal override string ParseBody(string source)
        {
            int start, end;
            string startMarker = @"<div class=""entry_body_text"">";
            start = source.IndexOf(startMarker);
            if (start < 0)
                throw new ApplicationException("Problem parsing HuffPo body");
            start += startMarker.Length;
            end = source.IndexOf(@"<span class=""get_huffpo"">", start);
            if (end < 0)
                end = source.IndexOf("<!-- amazon items -->", start);
            if (end < 0)
                end = source.IndexOf("<!-- /entry_body_text -->", start);
            if (end < 0)
                end = source.IndexOf("<!--end of content-->", start);
            if (end < 0)
                end = source.IndexOf("</div>", start);

            if (end < start)
                return source.Substring(start);

            return source.Substring(start, end - start);
        }
    }
}
